package utils

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
)

func HttpserverTest1() {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		readAll, _ := ioutil.ReadAll(r.Body)
		fmt.Printf("读取到的:%v\n", string(readAll))
		w.Write([]byte("codestorage v1"))

	})
	http.HandleFunc("/bye", sayBye)
	log.Println("Starting v1 server ...")
	log.Fatal(http.ListenAndServe(":1210", nil))
}

func sayBye(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("bye bye ,this is v1 httpServer"))
}

func HttpserverTest2() {
	mux := http.NewServeMux()
	mux.Handle("/", &myHandler{})
	mux.HandleFunc("/bye", sayBye2)

	log.Println("Starting v2 codestorage")
	log.Fatal(http.ListenAndServe(":1210", mux))
}

type myHandler struct{}

func (*myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("this is version 2"))
}
func sayBye2(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("bye bye ,this is v2 httpServer"))
}

//func HttpserverTest3() {
//	mux := http.NewServeMux()
//	mux.Handle("/", &myHandler{})
//	mux.HandleFunc("/bye", sayBye)
//
//	server := &http.Server{
//		Addr:         ":1210",
//		WriteTimeout: time.Second * 3,            //设置3秒的写超时
//		Handler:      mux,
//	}
//	log.Println("Starting v3 codestorage")
//	log.Fatal(server.ListenAndServe())
//}
//
//type myHandler struct{}
//
//func (*myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
//	w.Write([]byte("this is version 3"))
//}
//
//func sayBye(w http.ResponseWriter, r *http.Request) {
//	// 睡眠4秒  上面配置了3秒写超时，所以访问 “/bye“路由会出现没有响应的现象
//	time.Sleep(4 * time.Second)
//	w.Write([]byte("bye bye ,this is v3 httpServer"))
//}
